//
//  MYCreditsExchangeRecordVC.m
//  MuYing
//
//  Created by tbb on 2019/8/9.
//  Copyright © 2019 TBB. All rights reserved.
//
//完成
#import "MYCreditsExchangeRecordVC.h"

@interface MYCreditsExchangeRecordVC ()
@property (nonatomic, strong) UIView * leftLineView;

@property (strong, nonatomic) NSMutableArray *recordsArr;

@property (strong, nonatomic) YQPRecordsModel *model;
@end

@implementation MYCreditsExchangeRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.navigationItem.title = kYQLS(@"兑换记录");
    
    self.recordsArr = NSMutableArray.new;

    [self setupRefresh];
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserHomeCollectionsApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.recordsArr.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getUserHomeCollectionsApi:NO];
    }];
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma amrk - POST查询首页帖子列表
- (void)getUserHomeCollectionsApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);

    [self.recordsArr removeAllObjects];
    [YQPNetWorkingManager sendPostRequest:kPOST_INTERGER_EXCHANGE_LIST_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        self.model = [YQPRecordsModel mj_objectWithKeyValues:responseObject.data];
        [self.recordsArr addObjectsFromArray:self.model.list];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYCreditsExchangeRecordHeader");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYCreditsExchangeCell");
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recordsArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPRecordEntityModel *model = self.recordsArr[indexPath.row];
    MYCreditsExchangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYCreditsExchangeCell"];

    [cell setModel:model];
    UIView *lineView = [UIView new];
    lineView.backgroundColor = kAPPThemeColor;
    self.leftLineView = lineView;
    lineView.frame = CGRectMake(20, 0, 1, 30);
    [cell.contentView addSubview:lineView];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    MYCreditsExchangeRecordHeader *header= [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYCreditsExchangeRecordHeader"];
    UIView *lineView = [UIView new];
    lineView.backgroundColor = kAPPThemeColor;
    self.leftLineView = lineView;
    lineView.frame = CGRectMake(20, 21, 1, 30);

    [header addSubview:lineView];
    for (int i = 0; i < self.recordsArr.count; i++) {
        YQPRecordEntityModel *model = self.recordsArr[i];
        header.titleLb.text = [YQPInsureValidate compareCurrentTime:[YQPInsureValidate timeInStr:model.createTime]];

    }
    header.pointView.backgroundColor = kAPPThemeColor;
    header.titleLb.textColor = kAPPThemeColor;
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 41;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    self.leftLineView.frame = CGRectMake(20, 21, 1, scrollView.contentSize.height+kYQPScreenHeight);
    
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

@end
